<?php
date_default_timezone_set("UTC");
/**
 * config class
 *
 * @author K.Tamura <tamura@terras.co.jp>
 */
class tsConfig
{

    public $_configs = array();

    public function __construct( $file )
    {
        self::parse( $file );
    }

    /**
     * return this class instance
     *
     * @param string $file
     * @return object
     */
    public static function getInstance( $file )
    {
        static $instance;
        if( null === $instance )
        {
            $instance = new self( $file );
        }
        return $instance;
    }

    /**
     * parse file
     *
     * @param string $file
     * @return void
     */
    public function parse( $file )
    {
        if( true !== is_file( $file ) )
        {
            return array();
        }

        // parse ini file
        //
        $this->_configs = parse_ini_file( $file, true );
    }

    /**
     * return value
     *
     * @param string $group
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    public function get( $group, $key = null, $default = null )
    {
        if( true === isset( $this->_configs[$group] ) )
        {
            if( null !== $key )
            {
                if( true === isset( $this->_configs[$group][$key] ) )
                {
                    return $this->_configs[$group][$key];
                }
                else
                {
                    return $default;
                }
            }
            else
            {
                return $this->_configs[$group];
            }
        }
        else
        {
            return $default;
        }
    }

    /**
     * return all values
     *
     * @return array
     */
    public function &getAll()
    {
        return $this->_requests;
    }

    /**
     * has variable
     *
     * @param string $group
     * @param string $key
     * @return booleam
     */
    public function has( $group, $key = null )
    {
        return ( null !== $this->get( $group, $key ) ) ? true : false;
    }

}
